<?php

/**
 * Classe Promo
 * @author Remi
 * @version 0.5
 * @todo Finir la classe
 */
class Promo
{

    /**
     * @var int
     */
    private $Id;
    /**
     * @var string
     */
    private $Nom;

    /**
     *
     * @param int $id
     */
    public function __construct($id = 0)
    {
        if (isset($id) && !empty($id))
        {
            $sql = SQL::getInstance()->getConnection();

            $query = "	SELECT Promo.*
						FROM Promo
						WHERE Promo.IdPromo = :id
					";
            $c = $sql->prepare($query);
            $c->bindParam(":id", $id, PDO::PARAM_INT);

            if ($c->execute())
            {
                $result = $c->fetch(PDO::FETCH_OBJ);

                $this->Id = $result->IdPromo;
                $this->Nom = $result->NomPromo;

                $this->errormsg = "recuperee";
                $this->requeteOk = true;
            }
            else
            {
                $this->Id = 0;
                $this->Nom = "";

                $this->errormsg = "SQL Failed";
                $this->requeteOk = false;
            }
            $c->closeCursor();
        }
        else
        {
            $this->Id = 0;
            $this->Nom = "";

            $this->errormsg = "vierge";
        }
    }

    /**
     * Insert une promo dans la base
     */
    public function insert()
    {

    }

    /**
     * Met à jour le nom de la promo dans la base
     */
    public function update()
    {
        
    }

    /**
     * supprime une promo
     */
    public function delete()
    {
        $sql = SQL::getInstance()->getConnection();

        $query = 'DELETE FROM Promo WHERE IdPromo = :id';
        $c = $sql->prepare($query);
        $c->bindParam(':id', $this->Id, PDO::PARAM_INT);
        $c->execute();
    }

    /**
     *
     * @return int
     */
    public function getId()
    {
        return $this->Id;
    }

    /**
     *
     * @return string
     */
    public function getNom()
    {
        return $this->Nom;
    }

    /**
     *
     * @param string $nom
     * @return array
     */
    public function setNom($nom)
    {
        $errTab = array("boolOk" => true, "errmsg" => "");
        //Verif Type + verif Nulle, si bon mis a jour, sinon message erreur
        if (isset($nom) && !empty($nom) && strlen($nom) < 255)
        {
            $this->Nom = htmlentities($Nom);
            $this->errormsg = "";
        }
        else
        {
            $errTab["boolOk"] = false;
            $errTab["errmsg"] = "Le Nom défini";
            $this->Nom = "";
        }
        return $errTab;
    }

    /**
     *
     * @return array Array de promo
     */
    static public function getListPromo()
    {
        $sql = SQL::getInstance()->getConnection();

        $query = "SELECT * FROM Promo";
        $c = $sql->query($query);
        $result = $c->fetchAll(PDO::FETCH_OBJ);
        if (!empty($result))
        {
            $promo = array();
            foreach ($result as $k => $v)
            {
                $promo[$v->IdPromo] = $v->NomPromo;
            }
            return $promo;
        }
        return array();
    }

    /**
     * @param int $id
     * @return string
     */
    static public function getPromoById($id)
    {
        $sql = SQL::getInstance()->getConnection();

        $query = "SELECT NomPromo FROM Promo WHERE IdPromo = :id";
        $c = $sql->prepare($query);
        $c->bindParam(":id", $id, PDO::PARAM_INT);
        $c->execute();
        $result = $c->fetch(PDO::FETCH_OBJ);
        if (!empty($result))
        {
            return $result->NomPromo;
        }
        return "";
    }

}

?>